<template>
  <a-select
    show-search
    option-filter-prop="children"
    :placeholder="$t('pleaseSelect') + $t('province').toLowerCase()"
    :title="$t('pleaseSelect') + $t('province').toLowerCase()"
    :filter-option="filterOption"
    :allow-clear="true"
    :mode="mode"
    :maxTagCount="1"
    v-model="selectedValue"
    :disabled="disabled"

  >
    <a-select-option v-for="item in options" :key="item.id" :value="item.value">
      {{ item.name }}
    </a-select-option>
  </a-select>
</template>
<script>
import { mapState } from 'vuex'
export default {
  props: {
    defaultValue: {
      type: [String, Array],
      default () {
        return undefined
      }
    },
    mode: {
      type: String,
      default: 'default'
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    ...mapState({
      options: (state) => state.dict.provinceDictCode
    })
  },
  data () {
    return {
      selectedValue: []
    }
  },
  watch: {
    defaultValue: {
      handler (val) {
        if (val !== this.selectedValue) {
          this.selectedValue = this.defaultValue
        }
      },
      immediate: true
    },
    selectedValue (val) {
      this.$emit('changeoption', val)
    }
  },
  created () {
    this.getPageDictData()
  },
  methods: {
    // getData () {
    //   this.$store.dispatch('getProvinceDictCodeData')
    // },
    getPageDictData () {
      ['getProvinceDictCodeData'].forEach(method => {
        this.$store.dispatch(method)
      })
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    }
  }
}
</script>
